import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;


public class HTMLWriter {

	HashMap<String, BufferedWriter> packageWriters;
	
	public HTMLWriter(){
		packageWriters = new HashMap<String, BufferedWriter>();
	}
	
	private BufferedWriter beginHTML(String fileName, String title) throws IOException{
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(fileName));
			writer.write("<html>");
			writer.newLine();
			writer.write("<head>");
			writer.newLine();
			writer.write("<title>"+title+"</title>");
			writer.newLine();
			writer.write("</head>");
			writer.newLine();
			writer.write("<body>");
			writer.newLine();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
			
		return writer;
	}
	
	private void endHTML(BufferedWriter writer) throws IOException{
		writer.write("</body>");
		writer.newLine();
		writer.write("</html>");
		writer.newLine();
		
		writer.close();
	}
	
	private HashMap<String, String> extractID_NameTuple(HashMap<String, HashMap<String, String>> classAttributeCount){
		HashMap<String, String> tuple = new HashMap<String, String>();
		
		for (Entry<String,HashMap<String, String>> entryClass : classAttributeCount.entrySet()){
			for (Entry<String,String> entryAttributes : entryClass.getValue().entrySet()){
				if (entryAttributes.getKey().equals("name")){
					tuple.put(entryClass.getKey(), entryAttributes.getValue());
				}
			}
		}
		
		return tuple;
	}
	
	public void writePackagesRoot(ArrayList<String> packageNames){
		try {
			BufferedWriter writer = beginHTML("output/packages.html", "packages");
			
			for (String packageName : packageNames){
				writer.write("<a href=\""+packageName+".html\">"+packageName+"</a>");
				writer.write("<br/>");
				writer.newLine();
			}

			endHTML(writer);
			
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	
	public void writeClasses(HashMap<String, HashMap<String, String>> classAttributeCount){
		HashMap<String, String> id_NameTuple = extractID_NameTuple(classAttributeCount);
		try {
			File file = new File("output/classes/");
			file.mkdir();
			for (Entry<String,HashMap<String, String>> entryClass : classAttributeCount.entrySet()){
				//CLASS BEGIN
				BufferedWriter writer = beginHTML("output/classes/"+entryClass.getKey()+".html", "class "+entryClass.getKey());
				
				writer.write("<table border=\"1\">");
				writer.newLine();
				for (Entry<String,String> entryAttributes : entryClass.getValue().entrySet()){
					//ATRIBUTES FROM CLASS BEGIN
					writer.write("<tr>");
					writer.newLine();
					writer.write("<td>"+entryAttributes.getKey()+"</td>");
					writer.newLine();
					
					if (entryAttributes.getKey().equals("supertypes")){
						String[] supertypes = entryAttributes.getValue().split(" ");
						writer.write("<td>");
						writer.newLine();
						for (int i=0; i< supertypes.length; i++){
							writer.write("<a href=\""+supertypes[i]+".html\">"+id_NameTuple.get(supertypes[i])+ (i == supertypes.length-1 ? "" : ", ")+"</a>");
							writer.newLine();
						}
						writer.write("</td>");
						writer.newLine();
					}
					else if (entryAttributes.getKey().equals("package")){
						//FIRST WRITE CLASS IN PACKAGE HTML
						
						if (!packageWriters.containsKey(entryAttributes.getValue())){
							BufferedWriter packageWriter = beginHTML("output/"+entryAttributes.getValue()+".html", "package "+entryClass.getKey());
							packageWriters.put(entryAttributes.getValue(), packageWriter);
						}
						
						BufferedWriter packageWriter = packageWriters.get(entryAttributes.getValue());
						packageWriter.write("<a href=\"classes/"+entryClass.getKey()+".html\">"+id_NameTuple.get(entryClass.getKey())+"</a>");
						packageWriter.write("<br/>");
						packageWriter.newLine();
						
						//THEN WRITE PAGE LINK IN CLASS
						writer.write("<td>");
						writer.newLine();
						
						writer.write("<a href=\"../"+entryAttributes.getValue()+".html\">"+entryAttributes.getValue()+"</a>");
						
						writer.write("</td>");
						writer.newLine();
						
					}
					else{
						writer.write("<td>"+entryAttributes.getValue()+"</td>");
						writer.newLine();
					}
					writer.write("</tr>");
					writer.newLine();
					
					//ATTRIBUTES END
				}
				writer.write("</table>");
				writer.newLine();
				
				endHTML(writer);
				//CLASS END
			}
			
			for (BufferedWriter packagesWriter : packageWriters.values()){
				endHTML(packagesWriter);
			}

			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}
